using System.Web.Mvc;
using OurWedding1.Models;

namespace OurWedding1.Controllers
{   

    [Authorize]
    public class ContractorsController : Controller
    {
		private readonly IContractorRepository _contractorRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public ContractorsController() : this(new ContractorRepository())
        {
        }

        public ContractorsController(IContractorRepository contractorRepository)
        {
			_contractorRepository = contractorRepository;
        }

        //
        // GET: /Contractors/

        public ViewResult Index()
        {
            return View(_contractorRepository.AllIncluding(contractor => contractor.Expenses));
        }

        //
        // GET: /Contractors/Details/5

        public ViewResult Details(int id)
        {
            return View(_contractorRepository.Find(id));
        }

        //
        // GET: /Contractors/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Contractors/Create

        [HttpPost]
        public ActionResult Create(Contractor contractor)
        {
            if (ModelState.IsValid) {
                _contractorRepository.InsertOrUpdate(contractor);
                _contractorRepository.Save();
                return RedirectToAction("Index");
            }
            return View();
        }

        //
        // GET: /Contractors/Edit/5
 
        public ActionResult Edit(int id)
        {
             return View(_contractorRepository.Find(id));
        }

        //
        // POST: /Contractors/Edit/5

        [HttpPost]
        public ActionResult Edit(Contractor contractor)
        {
            if (ModelState.IsValid) {
                _contractorRepository.InsertOrUpdate(contractor);
                _contractorRepository.Save();
                return RedirectToAction("Index");
            }
            return View();
        }

        //
        // GET: /Contractors/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(_contractorRepository.Find(id));
        }

        //
        // POST: /Contractors/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            _contractorRepository.Delete(id);
            _contractorRepository.Save();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                _contractorRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

